package com.atguigu.gmall.weball.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.feign.item.ItemFeign;
import com.atguigu.gmall.feign.product.ProductFeign;
import com.atguigu.gmall.model.to.SkuDetailTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.math.BigDecimal;

/**
 * @author A
 * @version 1.0
 * @description: TODO
 * @date 2025/8/29 19:42
 */
@Controller

public class ItemController {
    @Autowired
    private ItemFeign itemFeign;
    @Autowired
    private ProductFeign productFeign;

    @GetMapping("/{skuId}.html")
    public String item(@PathVariable("skuId") Long skuId, Model model) {
        Result<SkuDetailTo> skuDetail = itemFeign.getSkuDetail(skuId);


        if (skuDetail.isOk()) {
            SkuDetailTo skuDetailTo = skuDetail.getData();

            model.addAttribute("categoryView", skuDetailTo.getCategoryView());
            model.addAttribute("skuInfo", skuDetailTo.getSkuInfo());

            model.addAttribute("spuSaleAttrList", skuDetailTo.getSpuSaleAttrList());
            model.addAttribute("valuesSkuJson", skuDetailTo.getValuesSkuJson());
        }
        //价格经常变动，所以不适合缓存
        BigDecimal skuInfoPrice = productFeign.getSkuInfoPrice(skuId);

        model.addAttribute("price", skuInfoPrice);
        return "item/index";
    }
}
